package com.geekaca.d07.test_stream;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class SteamDemo3 {
    static double allMoney;
    static double allMoneyTotal;

    public static void main(String[] args) {
        List<Employee> employeeList1 = new ArrayList<>();
        employeeList1.add(new Employee("猪八戒", '男', 30000, 25000, null));
        employeeList1.add(new Employee("孙悟空", '男', 25000, 2000, "顶撞上司"));
        employeeList1.add(new Employee("沙僧", '男', 20000, 20000, ""));
        employeeList1.add(new Employee("小白龙", '男', 20000, 25000, ""));

        List<Employee> employeeList2 = new ArrayList<>();
        employeeList2.add(new Employee("武松", '男', 15000, 9000, null));
        employeeList2.add(new Employee("李逵", '男', 20000, 10000, null));
        employeeList2.add(new Employee("西门庆", '男', 50000, 100000, "被打"));
        employeeList2.add(new Employee("潘金莲", '女', 3500, 1000, "被打"));
        employeeList2.add(new Employee("武大郎", '男', 20000, 0, "被下毒"));

//        System.out.println(employeeList1.stream().max((o1, o2) -> (int) (o1.getSalary() + o1.getBonus() - o2.getSalary() - o2.getBonus())));
        Optional<Employee> employeeTopMoney = employeeList1.stream().max((o1, o2) -> Double.compare(o1.getBonus() + o1.getSalary(),
                o2.getBonus() + o2.getSalary()));

        System.out.println("研发一部收入最高的是: " + employeeTopMoney);

        /**
         * 找到研开一部中收入最高的人
         * 构造利用他的信息构造一个对象Topperformer
         */
//        Topperformer topperformer =
        Optional<Object> topperformer = employeeList1.stream().max((o1, o2) -> Double.compare(o1.getBonus() + o1.getSalary(),
                o2.getBonus() + o2.getSalary())).map(emp -> new Topperformer(emp.getName(),
                emp.getSalary() + emp.getBonus()));
        System.out.println(topperformer);

        /**
         * 2 研发一部,统计平均工资,去掉最高工资和最低工资
         */

        System.out.println("研发一部收入排序");
        employeeList1.stream().sorted((e1, e2) -> Double.compare(e1.getSalary() + e1.getBonus(),
                        e2.getSalary() + e2.getBonus()))
                .skip(1)// 去掉了一个最低的
                .limit(employeeList1.size() - 2)
                .forEach(e -> {
                    System.out.println(e);
                    allMoney += (e.getBonus() + e.getSalary());
                });

        // 3.求工资总和
        Stream<Employee> s1 = employeeList1.stream();
        Stream<Employee> s2 = employeeList2.stream();

        Stream<Employee> s12 = Stream.concat(s1, s2);

        s12.sorted((e1, e2) -> Double.compare(e1.getSalary() + e1.getBonus(), e2.getSalary() + e2.getBonus()))
                .skip(1) // 掐头
                .limit(employeeList1.size()+employeeList2.size()-2)
                .forEach(e->allMoneyTotal += (e.getSalary()+e.getBonus()));

        BigDecimal allMoneyDe = BigDecimal.valueOf(allMoneyTotal);
        BigDecimal employeeCnt = BigDecimal.valueOf(employeeList1.size() + employeeList2.size()-2);
        BigDecimal avgTotalMoney = allMoneyDe.divide(employeeCnt,2, RoundingMode.HALF_UP);
        System.out.println("整个研发部平均工资:"+avgTotalMoney);
    }
}
